﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.Services;
using BTGISWASECO.DataLayer;
using BTGISWASECO.BusinessEntities;
using BTGISWASECO.BusinessLayer;

public partial class PresentationLayer_Default : System.Web.UI.Page
{
    OngPhanPhoiBLL bll = new OngPhanPhoiBLL();
    DMHuongDongChayDAL DMHuongDongChayDal = new DMHuongDongChayDAL();
    OngPhanPhoiEntities ongPhanPhoiEntities = new OngPhanPhoiEntities();

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            if (Session["DataTable"] == "")
                Response.Redirect("../default.aspx");
             for (int i = DateTime.Now.Year; i >= 1990; --i) {
                  ListItem iteml = new ListItem(i.ToString(), i.ToString());            
                    DListNamSx.Items.Add(iteml);
                    DListNamLD.Items.Add(iteml);
                }
            DListNamSx.SelectedValue = ongPhanPhoiEntities.NamSanXuat.ToString();
            DListNamLD.SelectedValue = ongPhanPhoiEntities.NamLapDat.ToString();
            ongPhanPhoiEntities = bll.GetOngPhanPhoiEntitiesByID();
            txtObjectID.Value = ongPhanPhoiEntities.OBJECTID.ToString();
            txtMaDoanOng.Text = ongPhanPhoiEntities.IDMaDoanOng;
            this.txtDMA.Text = ongPhanPhoiEntities.IDDMA;
            this.txtDMZ.Text = ongPhanPhoiEntities.IDDMZ;
            foreach (var itemc in DMHuongDongChayDAL.GetList())
            {
                if (ongPhanPhoiEntities.HuongDongChayText.Trim() == itemc.MoTa.ToString().Trim())
                    ongPhanPhoiEntities.HuongDongChay = itemc.Ma;
                ListItem iteml = new ListItem(itemc.MoTa.ToString(), itemc.Ma.ToString());
                DListHDChay.Items.Add(iteml);
            }

            DListHDChay.SelectedValue = ongPhanPhoiEntities.HuongDongChay.ToString();
            // this.DListHDChay.DataSource = DMHuongDongChayDal.GetDMHuongDongChayEntitiesByID();
            this.nChieuDai.Text = ongPhanPhoiEntities.ChieuDai.ToString();
            this.nDKNgoai.Text = ongPhanPhoiEntities.DKNgoai.ToString();
            this.nDKTrong.Text = ongPhanPhoiEntities.DKTrong.ToString();
            this.lSLKPSuCo.Text = ongPhanPhoiEntities.SoLanSuCo.ToString();
            this.txtNguoiCNC.Text = ongPhanPhoiEntities.NguoiCNCuoi;
            this.txtNgayCNC.Text = ongPhanPhoiEntities.NgayCNCuoi.ToString("dd/MM/yyyy");
            this.txtDoNhamDD.Text = ongPhanPhoiEntities.DoNhamDanhDinh.ToString();
            this.txtDoNhamTT.Text = ongPhanPhoiEntities.DoNhamThucTe.ToString();
            this.dApLucHD.Text = ongPhanPhoiEntities.ALHoatDong.ToString();
            this.dApLucTK.Text = ongPhanPhoiEntities.ApLucThietKe.ToString();
            this.txtDiemCuoi.Text = ongPhanPhoiEntities.DiemCuoi;
            this.txtDiemDau.Text = ongPhanPhoiEntities.DiemDau;
            this.dDoSau.Text = ongPhanPhoiEntities.DoSau.ToString();
            this.dDoDoc.Text = ongPhanPhoiEntities.DoDoc.ToString();
            this.txtMaHinhAnh.Text = ongPhanPhoiEntities.IDHinhAnh;
            this.txtGhiChu.Text = ongPhanPhoiEntities.GhiChu;
            //DateTime dob = DateTime.Parse(Request.Form[txtNgayCNC.UniqueID]); DMLoaiDonVi
            foreach (var itemk in DMKichCoOngDAL.GetList())
            {
                if (ongPhanPhoiEntities.CoOngText.Trim() == itemk.MoTa.ToString().Trim())
                    ongPhanPhoiEntities.CoOng = itemk.Ma;
                ListItem iteml = new ListItem(itemk.MoTa.ToString(), itemk.Ma.ToString());
                DListKCOng.Items.Add(iteml);
            }
            if (ongPhanPhoiEntities.CoOng != null)
            DListKCOng.SelectedValue = ongPhanPhoiEntities.CoOng.ToString();

            foreach (var itemvl in DMVatLieuDAL.GetList())
            {
                if (ongPhanPhoiEntities.VatLieuText.Trim() == itemvl.MoTa.ToString().Trim())
                    ongPhanPhoiEntities.VatLieu = itemvl.Ma;
                ListItem iteml = new ListItem(itemvl.MoTa.ToString(), itemvl.Ma.ToString());
                DListVatLieu.Items.Add(iteml);
            }
            DListVatLieu.SelectedValue = ongPhanPhoiEntities.VatLieu.ToString();

            foreach (var itemtc in DMTieuChuanOngNuocDAL.GetList())
            {
                if (ongPhanPhoiEntities.TieuChuanText.Trim() == itemtc.MoTa.ToString().Trim())
                    ongPhanPhoiEntities.TieuChuan = itemtc.Ma;
                ListItem iteml = new ListItem(itemtc.MoTa.ToString(), itemtc.Ma.ToString());
                DListTCVatLieu.Items.Add(iteml);
            }
            DListTCVatLieu.SelectedValue = ongPhanPhoiEntities.TieuChuan.ToString();

            foreach (var itemnsx in DMNhaSanXuatDAL.GetList())
            {
                if (ongPhanPhoiEntities.NhaSanXuatText.Trim() == itemnsx.MoTa.ToString().Trim())
                    ongPhanPhoiEntities.NhaSanXuat = itemnsx.Ma;
                ListItem iteml = new ListItem(itemnsx.MoTa.ToString(), itemnsx.Ma.ToString());
                DListNhaSX.Items.Add(iteml);
            }
            DListNhaSX.SelectedValue = ongPhanPhoiEntities.NhaSanXuat.ToString();

            foreach (var itemnusx in DMNuocSanXuatDAL.GetList())
            {
                if (ongPhanPhoiEntities.NuocSanXuatText.Trim() == itemnusx.MoTa.ToString().Trim())
                    ongPhanPhoiEntities.NuocSanXuat = itemnusx.Ma;
                ListItem iteml = new ListItem(itemnusx.MoTa.ToString(), itemnusx.Ma.ToString());
                DListNuocSX.Items.Add(iteml);
            }
            DListNuocSX.SelectedValue = ongPhanPhoiEntities.NuocSanXuat.ToString();

            foreach (var itemnttsd in DMTinhTrangSDDAL.GetList())
            {
                if (ongPhanPhoiEntities.TinhTrangText.Trim() == itemnttsd.MoTa.ToString().Trim())
                    ongPhanPhoiEntities.TinhTrang = itemnttsd.Ma;
                ListItem iteml = new ListItem(itemnttsd.MoTa.ToString(), itemnttsd.Ma.ToString());
                DListTTSD.Items.Add(iteml);
            }
            DListTTSD.SelectedValue = ongPhanPhoiEntities.TinhTrang.ToString();

            foreach (var itemntldv in DMLoaiDonViDAL.GetList())
            {
                if (ongPhanPhoiEntities.DonViTKText != null)
                if (ongPhanPhoiEntities.DonViTKText.Trim() == itemntldv.MoTa.ToString().Trim())
                    ongPhanPhoiEntities.DonViTK= itemntldv.Ma;
                ListItem iteml = new ListItem(itemntldv.MoTa.ToString(), itemntldv.Ma.ToString());
                DListDonViTK.Items.Add(iteml);
            }
            DListDonViTK.SelectedValue = ongPhanPhoiEntities.DonViTK.ToString();

            foreach (var itemntldvtc in DMLoaiDonViDAL.GetList())
            {
                 if (ongPhanPhoiEntities.DonViTCText!=null)
                     if (ongPhanPhoiEntities.DonViTCText.Trim() == itemntldvtc.MoTa.ToString().Trim())
                         ongPhanPhoiEntities.DonViTC = itemntldvtc.Ma;
                 ListItem iteml = new ListItem(itemntldvtc.MoTa.ToString(), itemntldvtc.Ma.ToString());
                DListDonViTC.Items.Add(iteml);
            }
            DListDonViTC.SelectedValue = ongPhanPhoiEntities.DonViTC.ToString();

            foreach (var itemntldvgs in DMLoaiDonViDAL.GetList())
            {
                if (ongPhanPhoiEntities.DonViGSText != null)
                    if (ongPhanPhoiEntities.DonViGSText.Trim() == itemntldvgs.MoTa.ToString().Trim())
                        ongPhanPhoiEntities.DonViGS = itemntldvgs.Ma;
                ListItem iteml = new ListItem(itemntldvgs.MoTa.ToString(), itemntldvgs.Ma.ToString());
                DListDonViGS.Items.Add(iteml);
            }
            DListDonViGS.SelectedValue = ongPhanPhoiEntities.DonViGS.ToString();
            this.txtLoaiVatLieu.Text = ongPhanPhoiEntities.LoaiVatLieu;
            //DListDonViGS.DataSource = DListDonViTK.DataSource;
        }
    }
    #region phuong thuc
    // [WebMethod]
    //public static bool EdictOngPhanPhoi(int objectID, string IDMaDoanOng, string IDMZ, string IDDMA, int HuongDongChay, double ChieuDai)
    //{

    //    OngPhanPhoiEntities bk = new OngPhanPhoiEntities();
    //    OngPhanPhoiDAL ongPhanPhoiDAL = new OngPhanPhoiDAL();
    //    bk.OBJECTID = objectID;
    //    bk.IDMaDoanOng = IDMaDoanOng;
    //    bk.IDDMZ = IDMZ;
    //    bk.IDDMA = IDDMA;
    //    bk.HuongDongChay = HuongDongChay;
    //    bk.ChieuDai = ChieuDai;
    //    ongPhanPhoiDAL.Update(bk.OBJECTID, bk.IDMaDoanOng, bk.IDDMA, bk.HuongDongChay, bk.ChieuDai);
    //    return true;
    //}
    #endregion phuong thuc
    protected void btnCapNhat_Click(object sender, EventArgs e)
     {
         OngPhanPhoiEntities bk = new OngPhanPhoiEntities();
         OngPhanPhoiDAL ongPhanPhoiDAL = new OngPhanPhoiDAL();
         //OngPhanPhoiEntities ongPhanPhoiEntities = new OngPhanPhoiEntities(Convert.ToInt32(DListDonViTC.SelectedValue = DListDonViTC.SelectedValue!=""?DListDonViTC.SelectedValue:"0"),
         //    Convert.ToInt32(DListDonViGS.SelectedValue = DListDonViGS.SelectedValue!=""?DListDonViGS.SelectedValue:"0"), this.txtNguoiCNC.Text, DateTime.Parse(this.txtNgayCNC.Text.ToString()), this.txtGhiChu.Text, this.txtMaHinhAnh.Text);
         OngPhanPhoiEntities ongPhanPhoiEntities = new OngPhanPhoiEntities(Convert.ToInt32(txtObjectID.Value), this.txtMaDoanOng.Text,
            this.txtDMA.Text, this.txtDMZ.Text, Convert.ToInt32(DListHDChay.SelectedValue = DListHDChay.SelectedValue != "" ? DListHDChay.SelectedValue : "0"), Convert.ToDouble(this.nChieuDai.Text),
            Convert.ToDouble(this.nDKTrong.Text), Convert.ToDouble(this.nDKNgoai.Text), DListKCOng.SelectedValue = DListKCOng.SelectedValue != "" ? DListKCOng.SelectedValue : "",
            Convert.ToInt32(DListVatLieu.SelectedValue = DListVatLieu.SelectedValue != "" ? DListVatLieu.SelectedValue : "0"), Convert.ToInt32(DListTCVatLieu.SelectedValue = DListTCVatLieu.SelectedValue != "" ? DListTCVatLieu.SelectedValue : "0"),
            Convert.ToInt32(DListNhaSX.SelectedValue = DListNhaSX.SelectedValue != "" ? DListNhaSX.SelectedValue : "0"), Convert.ToInt32(DListNuocSX.SelectedValue = DListNuocSX.SelectedValue != "" ? DListNuocSX.SelectedValue : "0"),
            Convert.ToInt32(DListNamSx.SelectedValue), Convert.ToInt32(DListNamLD.SelectedValue),
            Convert.ToDouble(this.txtDoNhamDD.Text), Convert.ToDouble(this.txtDoNhamTT.Text),
            Convert.ToDouble(this.dApLucHD.Text), Convert.ToDouble(this.dApLucTK.Text),
            this.txtDiemCuoi.Text, this.txtDiemDau.Text, Convert.ToDouble(this.dDoSau.Text),
            Convert.ToDouble(this.dDoDoc.Text), Convert.ToInt32(DListTTSD.SelectedValue = DListTTSD.SelectedValue != "" ? DListTTSD.SelectedValue : "0"),
            Convert.ToInt32(this.lSLKPSuCo.Text), Convert.ToInt32(DListDonViTK.SelectedValue = DListDonViTK.SelectedValue != "" ? DListDonViTK.SelectedValue : "0"),
            Convert.ToInt32(DListDonViTC.SelectedValue = DListDonViTC.SelectedValue != "" ? DListDonViTC.SelectedValue : "0"), Convert.ToInt32(DListDonViGS.SelectedValue = DListDonViGS.SelectedValue != "" ? DListDonViGS.SelectedValue : "0"),
            this.txtNguoiCNC.Text, DateTime.Parse(Request.Form.Get("txtNgayCNC")), this.txtGhiChu.Text, this.txtMaHinhAnh.Text,
            this.txtLoaiVatLieu.Text);
            ongPhanPhoiDAL.Update1(ongPhanPhoiEntities);
        ClientScript.RegisterStartupScript(typeof(Page), "MessagePopUp", "<script language='JavaScript'>alert('Cập nhật thành công'); window.location.href ='../Default.aspx';</script>");
         //ongPhanPhoiDAL.Update(Convert.ToInt32(txtObjectID.Value), txtMaDoanOng.Text, txtDMA.Text, Convert.ToInt32(DListHDChay.SelectedValue), Convert.ToDouble(this.nChieuDai.Text));
       //     Response.Write("");
       // Response.Redirect("../default.aspx");
        //  Response.Close();
     }
    protected void btnHuyBo_Click(object sender, EventArgs e)
    {
        ClientScript.RegisterStartupScript(typeof(Page), "MessagePopUp", "<script language='JavaScript'>alert('Hủy bỏ cập nhật?'); window.location.href ='../Default.aspx';</script>");
    }
}
